package com.tech.view.frame;

import com.tech.util.LogProcessUtil;
import com.tech.view.common.ResourcesUtil;
import com.tech.view.panel.IndexPanel;
import com.tech.view.panel.LeftNavigatePanel;
import com.tech.view.panel.PanelLifeCycle;
import com.tech.view.panel.panelframe.HeaderPanel;
import org.jdesktop.animation.transitions.ScreenTransition;
import org.jdesktop.animation.transitions.TransitionTarget;
import org.jdesktop.core.animation.timing.Animator;
import org.jdesktop.core.animation.timing.interpolators.AccelerationInterpolator;
import org.jdesktop.swingx.JXFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public class MainFrame extends JXFrame implements TransitionTarget {
	private final static Logger LOG = LogProcessUtil.getLogger(MainFrame.class.getName());
	public static int TOP_TITLE_BAR_HEIGHT = 62;
	// 首页面板一直存在
	private IndexPanel indexPanel;
	// 预设值，后面会根据窗口大小进行修改
	private int width = 1000;
	private int height = 700;
	private JPanel mainPanel;
	private JPanel containerPanel;
	private String title;
	private BufferedImage icon;
	/**
	 * 顶部标题栏面板
	 */
	private HeaderPanel headerPanel;
	/**
	 * 左侧导航面板
	 */
	private LeftNavigatePanel leftNavigatePanel;
	/**
	 * 当前显示面板
	 */
	private JComponent currentContentPanel;
	private JComponent newContentPanel;

	private ScreenTransition transition;

	public MainFrame() {
		initService();
		initComponents();
		setListeners();
		initView();
	}

	private void initService() {
	}

	private void initComponents() {
		// 初始化首页面板
		indexPanel = new IndexPanel(this);

		icon = ResourcesUtil.getImage(ResourcesUtil.Image.LOGO);
		mainPanel = new JPanel(new BorderLayout());
		headerPanel = new HeaderPanel(this, width, TOP_TITLE_BAR_HEIGHT, indexPanel.getHotPanel());

		leftNavigatePanel = new LeftNavigatePanel(height);
		containerPanel = new JPanel(new BorderLayout());
	}

	private void initView() {
		// 根据屏幕大小调节窗口大小
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		width = Math.min(width, (int) (screenSize.width * 0.8));
		height = Math.min(height, (int) (screenSize.height * 0.8));

		// 顶部面板
		//取消java默认标题栏
		setUndecorated(true);


		// 主体面板
		containerPanel.add(leftNavigatePanel, BorderLayout.WEST);

		// 所有面板
		mainPanel.setPreferredSize(new Dimension(width, height));
		mainPanel.add(headerPanel, BorderLayout.NORTH);
		mainPanel.add(containerPanel);

		// 动画过渡
		Animator animator = new Animator.Builder()
				.setInterpolator(new AccelerationInterpolator(0.2f, 0.4f))
				.setDuration(500, TimeUnit.MILLISECONDS)
				.build();
		transition = new ScreenTransition.Builder(containerPanel, this)
				.setAnimator(animator)
				.build();

		// 初次创建显示首页页面，面板宽度是 窗口宽度 - 左侧导航宽度
		switchContentPanel(indexPanel, true);

		getContentPane().add(mainPanel);
		pack();
	}

	/**
	 * 切换右侧面板
	 *
	 * @param newPanel 新面板
	 */
	public void switchContentPanel(JPanel newPanel) {
		switchContentPanel(newPanel, true);
	}

	public void switchContentPanel(JPanel newPanel, boolean isPutScrollPanel) {
		// 切换面板时执行 旧面板的 end 方法
		if (newContentPanel instanceof PanelLifeCycle) {
			// 先判断是否切换页面
			if (!((PanelLifeCycle) newContentPanel).end()) return;
		} else if (newContentPanel instanceof JScrollPane) {
			Component component = ((JScrollPane) newContentPanel).getViewport().getComponent(0);
			if (component instanceof PanelLifeCycle)
				((PanelLifeCycle) component).end();
		}
		// 执行新面板的init()方法
		if (newPanel instanceof PanelLifeCycle)
			((PanelLifeCycle) newPanel).start();

		// 设置面板的位置，将新面板放到滚动窗格中
		if (isPutScrollPanel) {
			JScrollPane scrollPane = new JScrollPane(newPanel);
			scrollPane.setBorder(BorderFactory.createEmptyBorder());
			newContentPanel = scrollPane;
		} else {
			newContentPanel = newPanel;
		}
		transition.start();
	}

	@Override
	public void setupNextScreen() {
		if (currentContentPanel != null) {
			containerPanel.remove(currentContentPanel);
		}
		containerPanel.add(newContentPanel);
		currentContentPanel = newContentPanel;
		// containerPanel.validate();
	}

	private void setListeners() {

	}

	public IndexPanel getIndexPanel() {
		return indexPanel;
	}

	public void setIndexPanel(IndexPanel indexPanel) {
		this.indexPanel = indexPanel;
	}

	public JComponent getCurrentContentPanel() {
		return currentContentPanel;
	}

	public void setCurrentContentPanel(JComponent currentContentPanel) {
		this.currentContentPanel = currentContentPanel;
	}
}
